package com.zzyl.service.impl;

import com.zzyl.constant.Constants;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.entity.Room;
import com.zzyl.entity.RoomType;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.mapper.RoomMapper;
import com.zzyl.mapper.RoomTypeMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private RoomTypeMapper roomTypeMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    @Override
    public void createBed(BedDto bedDto) {
        //校验参数
        if(Objects.isNull(bedDto.getSort()) || Objects.isNull(bedDto.getRoomId()) ||
                ObjectUtils.isEmpty(bedDto.getBedNumber())
        ){
            throw new BaseException(BasicEnum.BED_INSERT_FAIL);
        }
        //查询该房号是否存在
        Bed bedInDb =bedMapper.selectBybedNumber(bedDto.getBedNumber());
        if(Objects.nonNull(bedInDb)){
            throw new BaseException(BasicEnum.BED_INSERT_FAIL);
        }

        Bed bed = new Bed();
        BeanUtils.copyProperties(bedDto,bed);
        bed.setBedStatus(Constants.STATUS_ZERO);
        bedMapper.insert(bed);
    }

    @Override
    public BedVo getById(Integer id) {
         Bed bed=bedMapper.selectById(id);
         BedVo bedVo=new BedVo();
         BeanUtils.copyProperties(bed,bedVo);
        return bedVo;
    }

    @Override
    public void update(BedDto bedDto) {
        //校验排序是否为空，床位编号前端已经校验
        if(Objects.isNull(bedDto.getSort())){
            throw new BaseException(BasicEnum.BED_UPDATE_FAIL);
        }
        Bed bed =new Bed();
        BeanUtils.copyProperties(bedDto,bed);
        bedMapper.update(bed);
    }

    @Override
    public void delete(Integer id) {
        Bed bed = bedMapper.selectById(id);
        if(!Constants.STATUS_ZERO.equals(bed.getBedStatus())){
            throw new BaseException(BasicEnum.OLD_CHECKED_IN);
        }
        bedMapper.deleteById(id);
    }

}

